﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Gx.Common
{
    public class LogHelper
    {
        /// <summary>
        /// 创建文件夹并创建日志
        /// </summary>
        /// <param name="logName"></param>
        /// <returns></returns>
        private static string CreateFolder(string logName)
        {
            try
            {
                string folderUrl = System.Web.HttpContext.Current.Server.MapPath(string.Format("/Log/{0}", DateTime.Now.ToString("yyyyMM")));
                if (!Directory.Exists(folderUrl))
                {
                    Directory.CreateDirectory(folderUrl);
                }
                string fileUrl = string.Format("{0}/{1}", folderUrl, logName);
                if (!File.Exists(fileUrl))
                {
                    File.Create(fileUrl).Close();
                }
                return fileUrl;
            }
            catch
            {
                return "";
            }
        }



        /// <summary>
        /// 错误日志
        /// </summary>
        /// <param name="errorMessage"></param>
        public static void WriteErrorLog(string errorMessage)
        {
            string result = CreateFolder("error.txt");
            if (!string.IsNullOrEmpty(result))
            {
                using (FileStream fs = new FileStream(result, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    try
                    {
                        StreamWriter w = new StreamWriter(fs);
                        w.BaseStream.Seek(0, SeekOrigin.End);
                        w.Write("------------------------------------{0}--------------------------------------", DateTime.Now.ToStringyyyyMMddHHmmss());
                        w.Write("\r\n");
                        w.Write(errorMessage);
                        w.Write("\r\n");
                        w.Write("-----------------------------------------------------------------------------");
                        w.Flush();
                        w.Close();
                    }
                    finally
                    {
                        fs.Close();
                    }
                }
            }
        }

        /// <summary>
        /// 操作日志
        /// </summary>
        /// <param name="operationMessage"></param>
        public static void WriteOperationLog(string operationMessage)
        {
            string result = CreateFolder("opt.txt");
            if (!string.IsNullOrEmpty(result))
            {
                using (FileStream fs = new FileStream(result, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    try
                    {
                        StreamWriter w = new StreamWriter(fs);
                        w.BaseStream.Seek(0, SeekOrigin.End);
                        w.Write("------------------------------------{0}--------------------------------------", DateTime.Now.ToStringyyyyMMddHHmmss());
                        w.Write("\r\n");
                        w.Write(operationMessage);
                        w.Write("\r\n");
                        w.Write("-----------------------------------------------------------------------------");
                        w.Flush();
                        w.Close();
                    }
                    finally
                    {
                        fs.Close();
                    }
                }
            }
        }
    }
}
